--内容类型
ngx.header.content_type = "application/json;charset=utf8"
--加载模块，返回值模块常量
--openresty默认内嵌了lua_cjson模块，用来序列化数据
local cjson = require("cjson")
local mysql = require("resty.mysql")
--获得请求参数
local uri_args = ngx.req.get_uri_args()
--获得position
local position = uri_args["position"]
local db = mysql:new()
db:set_timeout(1000)
local props = {
    host = "106.75.138.203",
    port = 3306,
    database = "shop_business",
    user = "root",
    password = "leekawah123"
}
local res = db:connect(props)
local select_sql = "select url,image from tb_ad where status = 1 and position='" .. position .. "' and start_time<= NOW() AND end_time>= NOW()"
--执行查询
res = db:query(select_sql)
db:close()
--加载redis
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(2000)
local ip = "106.75.138.203"
local port = 6379
red:connect(ip,port)
red:auth("leekawah123")
red:set("ad_"..position,cjson.encode(res))
red:close()
ngx.say("{flag:true}")